<?php
// 应用公共文件

//设置缓存
use Ramsey\Uuid\Uuid;
use think\facade\Cache;

function setCache ($key, $value, $expire = 0, $u = '' )
{

//    $tag = 'shine_' . $u;
    $key = $key;

    return Cache::set( $key, $value, $expire );
}

//获取缓存
function getCache ( $key, $u = '' )
{
    if ( !hasCache( $key, $u ) ) return false;
    $key = $key;
    return Cache::get( $key );
}

//追加缓存
function pushCache ( $key, $value, $u = '' )
{
    $key = $key;
    return Cache::push( $key, $value );
}

//删除缓存
function delCache ( $key, $u = '' )
{
    $key = $key;
    return Cache::delete( $key );
}

//获取并删除缓存
function pullCache ( $key, $u = '' )
{
    $key = $key;
    return Cache::pull( $key );
}

//不存在则写入缓存数据后返回
function rememberCache ( $key, $value, $u = '' )
{
    $key = $key;
    return Cache::remember( $key, $value );
}

//清空缓存
function clearCache ( $u = '' )
{
    $tag = 'shine_' . $u;
    return Cache::tag( $tag )
        ->clear();
}

//缓存自增
function incCache ( $key, $step = 1, $u = '' )
{
    $key = $key;
    return Cache::inc( $key, $step );
}

//缓存自减
function decCache ( $key, $step = 1, $u = '' )
{
    $key = $key;
    return Cache::dec( $key, $step );
}

//判断缓存是否存在
function hasCache ( $key, $u = '' )
{
    $key = $key;
    return Cache::has( $key );
}

//通过Token获取用户信息
function getUserForToken ( $token )
{
    return getCache( "Token_" . $token );
}


//创建签名 (一个超级简单的签名)
function createSimpleSign ( $token, $data )
{
    //    $key1 = md5($token);
    //    $key2 = md5($data);
    //    $sign = md5($key1 . $key2 .$token);
    $sign = md5( $token . $data . $token );
    return $sign;
}

//根据Rsamy uuid 生成32位uuid
function uuid ()
{
    try
    {
        // Generate a version 1 (time-based) UUID object
        // Generate a version 3 (name-based and hashed with MD5) UUID object
        // Generate a version 4 (random) UUID object
        // Generate a version 5 (name-based and hashed with SHA1) UUID object
        $uuid1 = Uuid::uuid1();
        return str_replace( '-', '', $uuid1->toString() );
        // i.e. e4eaaaf2-d142-11e1-b3e4-080027620cdd
    }
    catch ( UnsatisfiedDependencyException $e )
    {
        // Some dependency was not met. Either the method cannot be called on a
        // 32-bit system, or it can, but it relies on Moontoast\Math to be present.
        error( 'Caught exception: ' . $e->getMessage(), 1100 );
    }
}

//获取文件地址
function getFilePath($path , $web_url ,$type = null)
{
    $website_url = $web_url . '/attachment';
    return $website_url . '/' .$path;
}